package com.rfid.rfidbluetooth;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DividerItemDecoration;

import com.rfid.base.base.BaseListActivity;
import com.rfid.base.base.BasePresenter;
import com.rfid.base.util.BaseUtil;
import com.rfid.rfidbluetooth.manager.BleClientManager;
import com.rfid.rfidbluetooth.manager.BluetoothManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RfidListActvitiy extends BaseListActivity<RfidAdapter,Rfid, BasePresenter> {

    @BindView(R.id.spinner)
    Spinner mSpinner;

    int mSelectPos = 1;

    @Override
    protected void onCreate(@Nullable  Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        List<String> phs = new ArrayList<>();
        for (int i=1;i<97;i++) {
            phs.add(i + "");
        }
        BleClientManager.INSTANCE.setRfidListener(new RfidListener() {
            @Override
            public void onRfids(List<Rfid> rfids) {
                mData.clear();
                mData.addAll(rfids);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mAdapter.notifyDataSetChanged();
                        onHideLoading();
                    }
                });
            }

            @Override
            public void onModifyEpcResult(boolean success) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(RfidListActvitiy.this,success?"修改成功":"修改失败",Toast.LENGTH_SHORT).show();
                        query(mSelectPos,phs);
                    }
                });
            }
        });
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        mSpinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,phs));
        mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                mSelectPos = position;
                query(position, phs);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }

    private void query(int position, List<String> phs) {
        BleClientManager.INSTANCE.reqRfids(1,Integer.valueOf(phs.get(position)));
        onShowLoading("正在查询");
    }


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_rfid_list;
    }

    @Override
    protected RfidAdapter getAdapter() {
        return new RfidAdapter(mData);
    }

    @Override
    protected int getRecyclerViewId() {
        return R.id.recyclerView;
    }

    @Override
    public BasePresenter getPresenter() {
        return null;
    }


    @Override
    public void onClick(Rfid rfid) {
        EditText editText = new EditText(this);
        editText.setText(rfid.getEpc());
        new AlertDialog.Builder(this)
                .setTitle("修改标签")
                .setView(editText)
                .setNegativeButton("取消",null)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        BleClientManager.INSTANCE.modifyEpc(rfid.getPh(),rfid.getPort(),editText.getText().toString());
                    }
                }).show();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.home,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.setting) {
            BaseUtil.startActivity(this,SettingActivity.class,null);
        }
        return super.onOptionsItemSelected(item);
    }
}
